# -*- coding: utf-8 -*-
"""
__mktime__ = '2019/4/8'
__author__ = 'Just'
__filename__ = 'iproxy'
"""
import threading
import re
from urllib import request
from ManhuaSpider.libs.user_agent import get_user_agent
import sys


lock = threading.Lock()
base_url = 'https://www.xicidaili.com/nn/'
root_pattern = r'<table id="ip_list">([\s\S]*?)</table>'
headers = {
    'User-Agent': get_user_agent()
}

start_page =1
max_page = 11


def get_proxy_list():
    """获取1000条代理ip记录"""
    countNum = 0
    proxyFile = open('proxy.txt', 'a', encoding='utf8')


    for page in range(start_page, max_page):
        _url = base_url + str(page)
        _request = request.Request(url=_url, headers=headers)
        html = request.urlopen(_request).read()
        html = str(html, encoding='utf-8')

        table_str = re.findall(root_pattern, html)[0]
        table_str = table_str.replace('\n', '').strip()
        list_str = table_str.split('</tr>')
        list_str = list_str[1:-1]
        tr_list = [item.strip() for item in list_str]
        ip_set = list()
        for item in tr_list:
            res = item.split('</td>')
            ip_str = res[1]
            port_str = res[2]
            _ip = '.'.join(re.findall('\d+', ip_str))
            _port = re.findall('\d+', port_str)[0]
            proxyFile.write('%s:%s\n' % (_ip, _port))
            countNum +=1

    proxyFile.close()
    return countNum


def verify_iproxy():
    """检验代理ip"""
    headers = {
        'User-Agent': get_user_agent()
    }
    test_url = 'https://www.baidu.com/'

    # TODO
    # 读取 proxy.txt文件内容
    # 使用iproxy 请求某站点 查看响应
    # 200 ok
